package com.mooral.web.tag.html;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts.Globals;
import org.apache.struts.taglib.logic.IterateTag;
import org.apache.struts.util.PropertyMessageResources;
import org.apache.struts.util.PropertyMessageResourcesFactory;

import com.mooral.bean.Bean;
import com.mooral.bean.Voucher;
import com.mooral.context.Context;
import com.mooral.context.ContextSession;
import com.mooral.web.util.SlugUtils;
import com.mooral.web.util.TagUtils;


public class VouchersTag extends IterateTag{

	TagUtils tagUtils = new TagUtils(); 

	public static String ORIENTATION_HORIZONTAL = "H";
	public static String ORIENTATION_VERTICAL = "V";
	
	protected String orientation;
	protected int amplitud; // TODO cambiar lenguaje
	protected Vector table[];
	protected int tableIdx;
	protected int tableLengthMax;
	protected String tableOpen;
	protected String tableClose;
	protected String bodyContentRetained;
	
/*	
    public VouchersTag() {
		super();
	}
*/
	
	public int doStartTag() throws JspException {

		/////////////////////////////////////////////
		id = "voucher";
		orientation=ORIENTATION_HORIZONTAL;
		amplitud=3; // TODO poner en 1
		table = new Vector[amplitud];
		for(int i = 0; i < amplitud ; i++){
			table[i] = new Vector();
		}
		tableIdx = 0;
		/////////////////////////////////////////////7
		
		
    	int ret = super.doStartTag();
    	
    	retainAndCleanBodyContent();

    	if(ret == EVAL_BODY_TAG){ // ITERA
    		writeTableOpen();
    		//writeCellOpen();
    	}
    	else{ // NO ITERA
    		
    	}

    	return ret;
    	//return SKIP_BODY;
    	
    }	

	public int doEndTag() throws JspException {
    	if(started){ // ITERO
    		writeTableClose();
    	}
    	
    	int ret = super.doEndTag();

    	writeAll();
    	
        return ret;
    }


	public int doAfterBody() throws JspException {
    	retainAndCleanBodyContent();
    	if(started){ // ITERO
    		writeCellClose();
    	}

    	int ret = super.doAfterBody();
    	
    	if(started){ // ITERO
    		writeCellOpen();
    	}
    	
        return ret;
    	//return SKIP_PAGE;
    }

    private void retainAndCleanBodyContent() throws JspException {
    	bodyContentRetained = (getBodyContent()==null?null:getBodyContent().getString()); 
/*    	try {
			getBodyContent().
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw new JspException(e);
		}*/
	}

	private void writeCellOpen() throws JspException {
    	
    	int mod = (lengthCount-1) % amplitud;
//    	System.out.println("mod:" + mod);

    	table[tableIdx].add(bodyContentRetained);
    	
    	if(mod != 0)tableIdx++;
    	else tableIdx = 0;
    	
    	
    	
    	
    	/*if()
  */  		
    	
/*		tagUtils.write(pageContext, "<tr>");
		tagUtils.write(pageContext, "<td>");
		tagUtils.write(pageContext, "lengthCount:" + lengthCount + " - ");*/
	}


    private void writeCellClose()  throws JspException {
		//System.out.println("writeCellClose:" + getBodyContent().getString());
    	
	//	tagUtils.write(pageContext, "</td>");
	//	tagUtils.write(pageContext, "</tr>");
		
	}	
    
    private void writeTableClose() throws JspException {
//		tableClose = "</table>";
	}

	private void writeTableOpen() throws JspException {
//		tableOpen = "<table width='100%' height='100%' border='3'>";
	}

	private void writeAll()  throws JspException {

		tagUtils.write(pageContext,"<table width=\"100%\" height=\"100%\" border=\"3\">");
		
		if(orientation.toUpperCase().equals(ORIENTATION_VERTICAL)){
			for(int i = 0; i < amplitud ; i++){
				tagUtils.write(pageContext,"<tr>");
				Iterator cells = table[i].iterator();
				while(cells.hasNext()){
					tagUtils.write(pageContext,"<td>");
					tagUtils.write(pageContext,cells.next().toString());
					tagUtils.write(pageContext,"</td>");
				}
				tagUtils.write(pageContext,"</tr>");
			}
		} else if(orientation.toUpperCase().equals(ORIENTATION_HORIZONTAL)){
			//int idx = 0;
			Iterator cells = table[0].iterator();
			//while(cells.hasNext()){
			for(int idx = 0;idx < table[0].size();idx++){
				tagUtils.write(pageContext,"<tr>");
				for(int i = 0; i < amplitud ; i++){
					tagUtils.write(pageContext,"<td>");
					tagUtils.write(pageContext,(idx < table[i].size()?table[i].elementAt(idx).toString():""));
					tagUtils.write(pageContext,"</td>");
				}
				tagUtils.write(pageContext,"</tr>");
				/*idx++;
				cells.next();*/
			}
		}
		
		tagUtils.write(pageContext,"<table/>");
	}

    public void release() {
        super.release();
    	
    	orientation = null;
    	table = null;
    	tableIdx = 0;
    	bodyContentRetained = null;
    }
}
